import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shoes_shop/api/api.dart';
import 'package:shoes_shop/core/const.dart';
import 'package:shoes_shop/core/flutter_icons.dart';
import 'package:shoes_shop/models/public_provider_model.dart';
import 'package:shoes_shop/pages/login/login_page.dart';
import 'home_page.dart';
import 'category_page.dart';
import 'cart_page.dart';
import './mine/mine_page.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> with WidgetsBindingObserver {
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
    if (state.toString() == 'AppLifecycleState.resumed') {
      getUserInfo(context);
    }
  }

  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(
        icon: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Icon(FlutterIcons.compass),
        ),
        label: 'data',
        tooltip: ""),
    BottomNavigationBarItem(
        icon: Icon(FlutterIcons.list), label: 'data', tooltip: ""),
    BottomNavigationBarItem(
        icon: Icon(FlutterIcons.bag), label: 'data', tooltip: ""),
    BottomNavigationBarItem(
        icon: Icon(FlutterIcons.person_outline), label: 'data', tooltip: ""),
  ];

  final List<Widget> tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MinePage()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30),
            topRight: Radius.circular(30),
          ),
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.black12,
              spreadRadius: 1,
              blurRadius: 10,
            )
          ],
        ),
        child: BottomNavigationBar(
          selectedItemColor: AppColors.deepBlue,
          unselectedItemColor: Colors.black26,
          currentIndex: Provider.of<PublicProviderModel>(context).currentIndex,
          type: BottomNavigationBarType.fixed,
          showSelectedLabels: false,
          showUnselectedLabels: false,
          backgroundColor: Colors.transparent,
          elevation: 0,
          items: bottomTabs,
          onTap: (index) {
            String openid =
                Provider.of<PublicProviderModel>(context, listen: false)
                    .userInfo['openid'];
            if (index == 2 && openid != null) {
              print('获取购物车列表');
              getCartList(context);
            }
            if (index == 3 && openid == null) {
              // 去登录页面
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => LoginPage(),
                ),
              );
            } else {
              Provider.of<PublicProviderModel>(context, listen: false)
                  .changeIndex(index);
            }
          },
        ),
      ),
      body: IndexedStack(
        index: Provider.of<PublicProviderModel>(context).currentIndex,
        children: tabBodies,
      ),
    );
  }
}
